package com.wg.web_app.service;

import java.util.ArrayList;
import java.util.List;

import com.wg.web_app.dao.SuperDaoFactory;
import com.wg.web_app.dao.table.*;
import com.wg.web_app.domin.DataBuilder;
import com.wg.web_app.domin.response.*;

public class UserService {
	

	
	public Response<User> isExist(int userid) {
		TableUser result = SuperDaoFactory.getUserDao().query(DataBuilder.tuser().id(userid).build());
		if(result != null){
			System.out.println("exist");
			User data = new User();
			data.setId(result.getId()); 
			data.setUserName(result.getUserName());
			return new Response<User>(ServiceConstants.STATUS_QUERY_SUCCESS,data);
		}else{
			System.out.println("not exist");
			User data = new User();
			return new Response<User>(ServiceConstants.STATUS_QUERY_FAIL,data);
		}
	}
	
	public Response<User> isExist(String loginname) {
		TableUser result =  SuperDaoFactory.getUserDao().query(DataBuilder.tuser().loginName(loginname).build());
		if(result != null){
			System.out.println("exist");
			User data = new User();
			data.setId(result.getId()); 
			data.setUserName(result.getUserName());
			return new Response<User>(ServiceConstants.STATUS_QUERY_SUCCESS,data);
		}else{
			System.out.println("not exist");
			User data = new User();
			return new Response<User>(ServiceConstants.STATUS_QUERY_FAIL,data);
		}
	}

	public Response<User> register(String loginname, String loginpass, String username) {
		TableUser result =  SuperDaoFactory.getUserDao().query(DataBuilder.tuser().loginName(loginname).build());
		if(result != null){
			return new Response<User>(ServiceConstants.STATUS_INSERT_DUP, new User());
		}else{
			int i =  SuperDaoFactory.getUserDao().insert(DataBuilder.tuser().loginName(loginname).loginPass(loginpass).userName(username).build());
			if(i > 0 )
				return new Response<User>(ServiceConstants.STATUS_INSERT_SUCCESS, DataBuilder.user().id(i).build());
			else 
				return new Response<User>(ServiceConstants.STATUS_INSERT_FAIL, new User());
		}
	}

	public Response<User> login(String username, String userpass) {
		TableUser result =  SuperDaoFactory.getUserDao().query(DataBuilder.tuser().loginName(username).loginPass(userpass).build());
		if(result != null){
			return new Response<User>(ServiceConstants.STATUS_QUERY_SUCCESS, DataBuilder.user().id(result.getId()).userName(result.getUserName()).build());
		}else{
			return new Response<User>(ServiceConstants.STATUS_QUERY_FAIL, new User());
		}
	}

	public Response<User> getNumbers(int id) {
		 List<TableUN> uns = SuperDaoFactory.getUnDao().querys(DataBuilder.tun().uid(id).build());
		 List<PhoneNumber> list = new ArrayList<PhoneNumber>();
		 for (TableUN un : uns) {
			list.add(new PhoneNumber(SuperDaoFactory.getNumberDao().query(DataBuilder.tnumber().id(un.getNumberId()).build())));
		}
		 return new Response<User>(ServiceConstants.STATUS_QUERY_SUCCESS,DataBuilder.user().id(id).numbers(list).build());
	}
	
	public Response<List<Group>> queryGroups(int id){
		 List<TableGU> gus = SuperDaoFactory.getGuDao().querys(DataBuilder.tgu().uid(id).build());
		 List<Group> list = new ArrayList<Group>();
		 for (TableGU gt : gus) {
			 if(gt.getStatus() > 0)
				 list.add(new Group(SuperDaoFactory.getGroupDao().query(DataBuilder.tgroup().id(gt.getGroupId()).build())));
		}
		 return new Response<List<Group>>(ServiceConstants.STATUS_QUERY_SUCCESS,list);
	}
}
